package _2获取资源的绝对路径;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

@WebServlet("/getpath")
public class GetFilePathServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context=this.getServletContext();
        String path=context.getRealPath("/WEB-INF/1.txt");
        Reader r=new FileReader(path);
        char [] buf=new char[1024];
        int len=0;
        while((len=r.read(buf))!=-1)
        {
            String l=new String (buf,0,len);
            resp.getWriter().println(l);
        }
    }
}
